// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Linq;
using System.Xml;
using System.IO;
using System.Reflection;

// ReSharper disable CheckNamespace
namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    internal static class Common
    {
        internal static bool CheckVersion(string lookingFor)
        {
            try
            {
                using (var wc = new System.Net.WebClient())
                {
                    wc.Proxy = UserSettings.Current.Proxy;
                    var xDoc = new System.Xml.XmlDocument();
                    string s = wc.DownloadString(@"http://download-codeplex.sec.s-msft.com/Download?ProjectName=wphonecommands&DownloadId=462447");
                    xDoc.LoadXml(s);

                    string newVersion = xDoc.DocumentElement.Attributes[lookingFor].Value;

                    Version vN = new Version(newVersion);
                    if (vN > Assembly.GetExecutingAssembly().GetName().Version)
                    {
                        return true;
                    }

                }
            }
            catch (Exception ex){ }
            return false;
        }

        internal static string FolderData()
        {
            var toolFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Constants.ApplicationFolder);
            if (!Directory.Exists(toolFolder))
                Directory.CreateDirectory(toolFolder);
            return toolFolder;
        }

        internal static string XmlEscape(string unescaped)
        {
            var doc = new XmlDocument();
            var node = doc.CreateElement("root");
            node.InnerText = unescaped;
            return node.InnerXml;
        }

        internal static string XmlUnescape(string escaped)
        {
            var doc = new XmlDocument();
            var node = doc.CreateElement("root");
            node.InnerXml = escaped;
            return node.InnerText;
        }

        internal static string FormatBytes(long bytes)
        {
            const int scale = 1024;
            var orders = new[] { "Gb", "Mb", "Kb", "Bytes" };
            var max = (long)Math.Pow(scale, orders.Length - 1);

            foreach (string order in orders)
            {
                if (bytes > max)
                    return String.Format("{0:##.##} {1}", Decimal.Divide(bytes, max), order);

                max /= scale;
            }
            return "0 Bytes";
        }

        internal static string GetIcon(IsoFilesView item)
        {
            if (item.IsDiretory)
            {
                return Constants.FolderClose;
            }
            var pos = item.Name.LastIndexOf(".", StringComparison.Ordinal);
            var ext = string.Empty;
            if (pos > 0)
            {
                ext = item.Name.Substring(pos + 1);
            }
            switch (ext.ToUpper())
            {
                case "SDF":
                    return Constants.ExtSdf;
                case "BMP":
                    return Constants.ExtBmp;
                case "DLL":
                    return Constants.ExtDll;
                case "DOC":
                case "DOCX":
                    return Constants.ExtDoc;
                case "EXE":
                    return Constants.ExtExe;
                case "GIF":
                    return Constants.ExtGif;
                case "HTML":
                case "HTM":
                    return Constants.ExtHtm;
                case "JPG":
                    return Constants.ExtJpg;
                case "PDF":
                    return Constants.ExtPdf;
                case "PNG":
                    return Constants.ExtPng;
                case "PPT":
                case "PPTX":
                    return Constants.ExtPpt;
                case "XLS":
                case "XLSX":
                    return Constants.ExtXls;
                case "XML":
                    return Constants.ExtXml;
            }
            return Constants.FileDefault;
        }

        internal static bool ValidNumericChars(string str)
        {
            return str.All(Char.IsNumber);
        }

        internal static bool XmlBoolConvert(string value, bool defaultvalue, bool optional)
        {
            if (value.ToLower() == "false" || value == "0") return false;
            if (value.ToLower() == "true" || value == "1") return true;
            if (string.IsNullOrEmpty(value) && optional) return defaultvalue;
            throw new InvalidCastException("Invalid cast to bool value : " + value);
        }

        internal static bool XmlBoolConvert(string value, bool defaultvalue)
        {
            return XmlBoolConvert(value, defaultvalue, false);
        }

        internal static string XmlBitPixelConvert(string value)
        {
            return String.IsNullOrEmpty(value) ? "16" : value;
        }

    }
}